package com.unswift.window.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.unswift.QuickApplication;
import com.unswift.constant.ButtonConstant;
import com.unswift.constant.QuickConstant;
import com.unswift.utils.ObjectUtils;
import com.unswift.window.extend.Attribute;
import com.unswift.window.factory.ButtonFactory;
import com.unswift.window.factory.FileChooseFactory;
import com.unswift.window.factory.FormFactory;
import com.unswift.window.factory.MessageFactory;
import com.unswift.window.factory.PanelFactory;
import com.unswift.window.factory.ScrollFactory;

@SuppressWarnings("serial")
public class GenericsPathConfigDialog extends QuickDialog{

	private int width;
	private JPanel projectPath;
	private JTextField projectDomain;
	private JTextField pojoName;
	private JTextField mapperName;
	private JTextField adapterName;
	private JTextField exportName;
	
	public GenericsPathConfigDialog(JFrame frame) {
		super(frame, "生成路径配置", false);
		
		setSize(frame);
		JPanel root = (JPanel)this.getContentPane();
		initForm(root);
		initBottom(root);
		setVisible(true);
	}
	
	private void setSize(JFrame frame) {
		int fwidth=frame.getWidth();
		int fheight=frame.getHeight();
		width=700;
		int height=350;
		int x=(fwidth-width)/2;
		int y=(fheight-height)/2;
		setBounds(x, y, width, height);
	}
	
	private void initForm(JPanel root){
		JPanel form=FormFactory.create(Color.WHITE, null, 2, 6, null);
		FormFactory.addRow(form, 0, "项目路径：", projectPath=FormFactory.createFormFile(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_FORM_VALUE, FileChooseFactory.getDefaultFile().getAbsolutePath()+"\\")
				.put(Attribute.ATTRIBUTE_FORM_EDITABLE, false)
				.put(Attribute.ATTRIBUTE_FILECHOOSE_TYPE, JFileChooser.DIRECTORIES_ONLY)
				.put(Attribute.ATTRIBUTE_FILECHOOSE_ENABLED, false)
				.put(Attribute.ATTRIBUTE_FORM_ACTION, ButtonConstant.ACTION_CREATE_PROJECT_PATH_FILE_SELECT)
		), null, null);
		FormFactory.addRow(form, 1, "域名：", projectDomain=FormFactory.createFormInput(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_FORM_VALUE, ObjectUtils.init(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_DOMAIN), ""))
				.put(Attribute.ATTRIBUTE_FORM_PLACEHOLDER, "com.xxx.cloud")
				.put(Attribute.ATTRIBUTE_FORM_MARGIN,  FormFactory.defaultMargin())
		), null, null);
		FormFactory.addRow(form, 2, "Pojo路径：", pojoName=FormFactory.createFormInput(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_FORM_VALUE, ObjectUtils.init(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_POJO), "cloud-010-pojo"))
			.put(Attribute.ATTRIBUTE_FORM_PLACEHOLDER, "cloud-010-pojo")
			.put(Attribute.ATTRIBUTE_FORM_MARGIN,  FormFactory.defaultMargin())
		), null, null);
		FormFactory.addRow(form, 3, "Mapper路径：", mapperName=FormFactory.createFormInput(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_FORM_VALUE, ObjectUtils.init(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_MAPPER), "cloud-015-mapper"))
				.put(Attribute.ATTRIBUTE_FORM_PLACEHOLDER, "cloud-015-mapper")
				.put(Attribute.ATTRIBUTE_FORM_MARGIN,  FormFactory.defaultMargin())
			), null, null);
		FormFactory.addRow(form, 4, "公共操作层路径：", adapterName=FormFactory.createFormInput(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_FORM_VALUE, ObjectUtils.init(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_ADAPTER), "cloud-020-adapter"))
				.put(Attribute.ATTRIBUTE_FORM_PLACEHOLDER, "cloud-020-adapter")
				.put(Attribute.ATTRIBUTE_FORM_MARGIN,  FormFactory.defaultMargin())
			), null, null);
		FormFactory.addRow(form, 5, "导出微服务路径：", exportName=FormFactory.createFormInput(
				Attribute.create()
				.put(Attribute.ATTRIBUTE_FORM_VALUE, ObjectUtils.init(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_EXPORT), "server-025-imexport"))
				.put(Attribute.ATTRIBUTE_FORM_PLACEHOLDER, "server-025-imexport")
				.put(Attribute.ATTRIBUTE_FORM_MARGIN,  FormFactory.defaultMargin())
			), null, null);
		root.add(ScrollFactory.addScroll(form, null, null));
	}
	
	public void initBottom(JPanel root){
		JPanel panel=PanelFactory.create(null, null, new FlowLayout(FlowLayout.RIGHT));
		panel.add(ButtonFactory.createDialogButton(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_BUTTON_NAME, "确定")
			.put(Attribute.ATTRIBUTE_BUTTON_ACTION, ButtonConstant.ACTION_DIALOG_CONFIRM)
			.put(Attribute.ATTRIBUTE_BUTTON_DEPEND, this)
		));
		panel.add(ButtonFactory.createDialogButton(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_BUTTON_NAME, "关闭")
			.put(Attribute.ATTRIBUTE_BUTTON_ACTION, ButtonConstant.ACTION_DIALOG_CLOSE)
			.put(Attribute.ATTRIBUTE_BUTTON_DEPEND, this)
		));
		root.add(panel, BorderLayout.SOUTH);
	}
	
	@Override
	public void confirm() {
		if(!validateForm()){
			return ;
		}
		String projectDomainText=projectDomain.getText();
		String pojoNameText=pojoName.getText();
		String mapperNameText=mapperName.getText();
		String adapterNameText=adapterName.getText();
		String exportNameText=exportName.getText();
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_DOMAIN, projectDomainText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_POJO, pojoNameText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_MAPPER, mapperNameText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_ADAPTER, adapterNameText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_EXPORT, exportNameText);
		this.close();
	}
	
	public boolean validateForm(){
		if(ObjectUtils.isEmpty(((JTextField)projectPath.getComponent(0)).getText())){
			MessageFactory.error(this, "项目路径不能为空");
			return false;
		}
		if(ObjectUtils.isEmpty(projectDomain.getText())){
			MessageFactory.error(this, "域名不能为空");
			return false;
		}
		if(!projectDomain.getText().matches("^[a-z]{2,}[a-z\\-0-9\\.]+$")) {
			MessageFactory.error(QuickApplication.getWindow(), "项目域名只能是小写字母、数字、-和.的组合，因为大写影响美观，请慎重");
			return false;
		}
		if(ObjectUtils.isEmpty(pojoName.getText())){
			MessageFactory.error(this, "Pojo路径不能为空");
			return false;
		}
		if(ObjectUtils.isEmpty(mapperName.getText())){
			MessageFactory.error(this, "Mapper路径不能为空");
			return false;
		}
		if(ObjectUtils.isEmpty(adapterName.getText())){
			MessageFactory.error(this, "adapter路径不能为空");
			return false;
		}
		if(ObjectUtils.isEmpty(exportName.getText())){
			MessageFactory.error(this, "导出微服务路径不能为空");
			return false;
		}
		return true;
	}
	
}
